/*
This file does the randomization inference for calculations reporting Young's procedure.
 
Inputs  
	Data "DataforAnalysis.dta" merged in file MergeData.do
 
Outputs
	indCoefTestBase
	jointTestBase

	indCoefTestPost
	jointTestPost.tex

	hp_jointTest_all.tex
	hp_indCoefTest_all.tex
	hpLev_jointTest_all.tex
	hpLev_indCoefTest_all.tex
	hpCalls_jointTest_all.tex
	hpCalls_indCoefTest_all.tex

 
*/

clear
set more off

cd  
global figurepath   
global datapath  
global tablepath 
global logpath
 
use "$datapath\DataforAnalysis.dta", clear
 

cap log close
log using "$logpath\RandInference.smcl", replace


*********************************************************************************
*************  TABLE Samples 
******************************************************************************** 

*Table 3 baseline analysis
	local baselinesample1   "if Week > -4 & Week < 5 & Treatment !=5"
	local baselinesample2   "if Week > -4 & Week < 5 & Treatment !=1"
	local baselinesample3   "if Week > -4 & Week < 5"
	local baselinesample4   "if Week > -4 & Week < 5"
*Table 4 persistence
	local persistencesample1   "if (Week >= -3 & Week <= 0 & Treatment !=5) | (Week >= 5 & Week <= 24 & Treatment !=5)  "
	local persistencesample2   "if (Week >= -3 & Week <= 0 & Treatment !=1) | (Week >= 5 & Week <= 24 & Treatment !=1) "
	local persistencesample3   "if (Week >= -3 & Week <= 0) | (Week >= 5 & Week <= 24) "
	local persistencesample4   "if (Week >= -3 & Week <= 0 & balancesample ==1) | (Week >= 5 & Week <= 24 & balancesample ==1)    "
	local persistencesample5   "if (Week >= -3 & Week <= 0) | (Week >= 5 & Week <= 24) "
	 
 

quietly tab Treatment, gen(TT)

* Set reps
local reps = 5000 


gen tp = Week>=1 & Week<=4
 
*------------------------------------------------------------------------------*
*** Table 2 baseline analysis
*------------------------------------------------------------------------------*

preserve
keep if Week<=4
capture drop internalcontrolpost pairincentivepost structuredmeetingpost combinedpost


matrix joint_base = J(1,5,.)
matrix coeff_base = J(3,5,.)
matrix joint_otherdv_base = J(1,6,.)

local outCol = 1
*Log revenue per call
**Internal control 	
randcmd ((pairincentive structuredmeeting combined) ///
	reghdfe lrpc  pairincentive structuredmeeting combined     `baselinesample1' , absorb(manager Week) cluster(manager))	///
	, treatvars(TT2 TT3 TT4) sample ///
    groupvar(manager) calc1(replace pairincentive = TT2*tp) ///
	calc2(replace structuredmeeting = TT3*tp) ///
	calc3(replace combined = TT4*tp) reps(`reps')
matrix eqn_p = e(REqn)
matrix joint_base[1,`outCol']= eqn_p[1,colnumb(eqn_p,"rand-t pvalue")]
matrix coeff_p= e(RCoef)
matrix coeff_base[1,`outCol']= coeff_p[rownumb(coeff_p,"structuredmeeting"),colnumb(coeff_p,"rand-t pvalue")]
matrix coeff_base[2,`outCol']= coeff_p[rownumb(coeff_p,"pairincentive"),colnumb(coeff_p,"rand-t pvalue")]
matrix coeff_base[3,`outCol']= coeff_p[rownumb(coeff_p,"combined"),colnumb(coeff_p,"rand-t pvalue")]
local outCol = `outCol'+1

**External control 

randcmd ((pairincentive structuredmeeting combined) ///
	reghdfe lrpc  pairincentive structuredmeeting combined     `baselinesample2' , absorb(manager Week) cluster(manager))	///
	, treatvars(TT2 TT3 TT4) sample ///
    groupvar(manager) calc1(replace pairincentive = TT2*tp) ///
	calc2(replace structuredmeeting = TT3*tp) ///
	calc3(replace combined = TT4*tp) reps(`reps')
matrix eqn_p = e(REqn)
matrix joint_base[1,`outCol']= eqn_p[1,colnumb(eqn_p,"rand-t pvalue")]
matrix coeff_p= e(RCoef)
matrix coeff_base[1,`outCol']= coeff_p[rownumb(coeff_p,"structuredmeeting"),colnumb(coeff_p,"rand-t pvalue")]
matrix coeff_base[2,`outCol']= coeff_p[rownumb(coeff_p,"pairincentive"),colnumb(coeff_p,"rand-t pvalue")]
matrix coeff_base[3,`outCol']= coeff_p[rownumb(coeff_p,"combined"),colnumb(coeff_p,"rand-t pvalue")]
local outCol = `outCol'+1
	
	**Both internal and external control 
randcmd ((pairincentive structuredmeeting combined) ///
	reghdfe lrpc  pairincentive structuredmeeting combined internalcontrol    `baselinesample3' , absorb(manager Week) cluster(manager))	///
	, treatvars(TT2 TT3 TT4) sample ///
    groupvar(manager) calc1(replace pairincentive = TT2*tp) ///
	calc2(replace structuredmeeting = TT3*tp) ///
	calc3(replace combined = TT4*tp) calc4(replace internalcontrol= TT1*tp) reps(`reps')
matrix eqn_p = e(REqn)
matrix joint_base[1,`outCol']= eqn_p[1,colnumb(eqn_p,"rand-t pvalue")]
matrix coeff_p= e(RCoef)
matrix coeff_base[1,`outCol']= coeff_p[rownumb(coeff_p,"structuredmeeting"),colnumb(coeff_p,"rand-t pvalue")]
matrix coeff_base[2,`outCol']= coeff_p[rownumb(coeff_p,"pairincentive"),colnumb(coeff_p,"rand-t pvalue")]
matrix coeff_base[3,`outCol']= coeff_p[rownumb(coeff_p,"combined"),colnumb(coeff_p,"rand-t pvalue")]
local outCol = `outCol'+1

 
	
**Both internal and external control individual fixed effects
randcmd ((pairincentive structuredmeeting combined) ///
	reghdfe lrpc     pairincentive structuredmeeting combined   internalcontrol   `baselinesample4' , absorb(ID Week) keepsingle cluster(manager))	///
	, treatvars(TT2 TT3 TT4) sample ///
    groupvar(manager) calc1(replace pairincentive = TT2*tp) ///
	calc2(replace structuredmeeting = TT3*tp) ///
	calc3(replace combined = TT4*tp) calc4(replace internalcontrol= TT1*tp) reps(`reps')	
matrix eqn_p = e(REqn)
matrix joint_base[1,`outCol']= eqn_p[1,colnumb(eqn_p,"rand-t pvalue")]
matrix coeff_p= e(RCoef)
matrix coeff_base[1,`outCol']= coeff_p[rownumb(coeff_p,"structuredmeeting"),colnumb(coeff_p,"rand-t pvalue")]
matrix coeff_base[2,`outCol']= coeff_p[rownumb(coeff_p,"pairincentive"),colnumb(coeff_p,"rand-t pvalue")]
matrix coeff_base[3,`outCol']= coeff_p[rownumb(coeff_p,"combined"),colnumb(coeff_p,"rand-t pvalue")]
local outCol = `outCol'+1

	**Both internal and external control, revenue DV 
randcmd ((pairincentive structuredmeeting combined) ///
	reghdfe revenue  pairincentive structuredmeeting combined internalcontrol    `baselinesample3' , absorb(manager Week) cluster(manager))	///
	, treatvars(TT2 TT3 TT4) sample ///
    groupvar(manager) calc1(replace pairincentive = TT2*tp) ///
	calc2(replace structuredmeeting = TT3*tp) ///
	calc3(replace combined = TT4*tp) calc4(replace internalcontrol= TT1*tp) reps(`reps')
matrix eqn_p = e(REqn)
matrix joint_base[1,`outCol']= eqn_p[1,colnumb(eqn_p,"rand-t pvalue")]
matrix coeff_p= e(RCoef)
matrix coeff_base[1,`outCol']= coeff_p[rownumb(coeff_p,"structuredmeeting"),colnumb(coeff_p,"rand-t pvalue")]
matrix coeff_base[2,`outCol']= coeff_p[rownumb(coeff_p,"pairincentive"),colnumb(coeff_p,"rand-t pvalue")]
matrix coeff_base[3,`outCol']= coeff_p[rownumb(coeff_p,"combined"),colnumb(coeff_p,"rand-t pvalue")]
local outCol = `outCol'+1


mat2txt , matrix(joint_base) saving("$tablepath\jointTestBase.tex") replace
mat2txt , matrix(coeff_base) saving("$tablepath\indCoefTestBase.tex") replace
 

 
*------------------------------------------------------------------------------*
** Other DVs, intervention period.
*------------------------------------------------------------------------------*


randcmd ((pairincentive structuredmeeting combined) ///
	reghdfe RPH  pairincentive structuredmeeting combined internalcontrol    `baselinesample3' , absorb(manager Week) cluster(manager))	///
	, treatvars(TT2 TT3 TT4) sample ///
    groupvar(manager) calc1(replace pairincentive = TT2*tp) ///
	calc2(replace structuredmeeting = TT3*tp) ///
	calc3(replace combined = TT4*tp) calc4(replace internalcontrol= TT1*tp) reps(`reps')
matrix eqn_p = e(REqn)
matrix joint_otherdv_base[1,1]= eqn_p[1,colnumb(eqn_p,"rand-t pvalue")]

randcmd ((pairincentive structuredmeeting combined) ///
	reghdfe lrph  pairincentive structuredmeeting combined internalcontrol    `baselinesample3' , absorb(manager Week) cluster(manager))	///
	, treatvars(TT2 TT3 TT4) sample ///
    groupvar(manager) calc1(replace pairincentive = TT2*tp) ///
	calc2(replace structuredmeeting = TT3*tp) ///
	calc3(replace combined = TT4*tp) calc4(replace internalcontrol= TT1*tp) reps(`reps')
matrix eqn_p = e(REqn)
matrix joint_otherdv_base[1,3]= eqn_p[1,colnumb(eqn_p,"rand-t pvalue")]


randcmd ((pairincentive structuredmeeting combined) ///
	reghdfe RPC  pairincentive structuredmeeting combined internalcontrol    `baselinesample3' , absorb(manager Week) cluster(manager))	///
	, treatvars(TT2 TT3 TT4) sample ///
    groupvar(manager) calc1(replace pairincentive = TT2*tp) ///
	calc2(replace structuredmeeting = TT3*tp) ///
	calc3(replace combined = TT4*tp) calc4(replace internalcontrol= TT1*tp) reps(`reps')
matrix eqn_p = e(REqn)
matrix joint_otherdv_base[1,5]= eqn_p[1,colnumb(eqn_p,"rand-t pvalue")] 
 
 
 
*------------------------------------------------------------------------------*
** Table 3 Persistence
*------------------------------------------------------------------------------*

restore
drop tp
gen tp = Week>4
 
drop if Week<=4 & Week>=1
 

rename  internalcontrol internalcontrol_boot
rename  pairincentive pairincentive_boot
rename  structuredmeeting structuredmeeting_boot
rename  combined combined_boot




* keep naming conventions constant to align matrix output.
rename internalcontrolpost internalcontrol 
rename pairincentivepost pairincentive 
rename structuredmeetingpost structuredmeeting 
rename combinedpost combined
 
matrix joint_base = J(1,6,.)
matrix coeff_base = J(3,6,.)

local outCol = 1
*Log revenue per call
**Internal control 	
randcmd ((pairincentive structuredmeeting combined) ///
	reghdfe lrpc  pairincentive structuredmeeting combined    `persistencesample1' , absorb(manager Week) cluster(manager))	///
	, treatvars(TT2 TT3 TT4) sample ///
    groupvar(manager) calc1(replace pairincentive = TT2*tp) ///
	calc2(replace structuredmeeting = TT3*tp) ///
	calc3(replace combined = TT4*tp) reps(`reps')
matrix eqn_p = e(REqn)
matrix joint_base[1,`outCol']= eqn_p[1,colnumb(eqn_p,"rand-t pvalue")]
matrix coeff_p= e(RCoef)
matrix coeff_base[1,`outCol']= coeff_p[rownumb(coeff_p,"structuredmeeting"),colnumb(coeff_p,"rand-t pvalue")]
matrix coeff_base[2,`outCol']= coeff_p[rownumb(coeff_p,"pairincentive"),colnumb(coeff_p,"rand-t pvalue")]
matrix coeff_base[3,`outCol']= coeff_p[rownumb(coeff_p,"combined"),colnumb(coeff_p,"rand-t pvalue")]
local outCol = `outCol'+1


**External control 
randcmd ((pairincentive structuredmeeting combined) ///
	reghdfe lrpc  pairincentive structuredmeeting combined    `persistencesample2' , absorb(manager Week) cluster(manager))	///
	, treatvars(TT2 TT3 TT4) sample ///
    groupvar(manager) calc1(replace pairincentive = TT2*tp) ///
	calc2(replace structuredmeeting = TT3*tp) ///
	calc3(replace combined = TT4*tp) reps(`reps')
matrix eqn_p = e(REqn)
matrix joint_base[1,`outCol']= eqn_p[1,colnumb(eqn_p,"rand-t pvalue")]
matrix coeff_p= e(RCoef)
matrix coeff_base[1,`outCol']= coeff_p[rownumb(coeff_p,"structuredmeeting"),colnumb(coeff_p,"rand-t pvalue")]
matrix coeff_base[2,`outCol']= coeff_p[rownumb(coeff_p,"pairincentive"),colnumb(coeff_p,"rand-t pvalue")]
matrix coeff_base[3,`outCol']= coeff_p[rownumb(coeff_p,"combined"),colnumb(coeff_p,"rand-t pvalue")]
local outCol = `outCol'+1




**Both internal and external control 

randcmd ((pairincentive structuredmeeting combined) ///
	reghdfe lrpc  pairincentive structuredmeeting combined  internalcontrol   `persistencesample3' , absorb(manager Week) cluster(manager))	///
	, treatvars(TT2 TT3 TT4) sample ///
    groupvar(manager) calc1(replace pairincentive = TT2*tp) ///
	calc2(replace structuredmeeting = TT3*tp) ///
	calc3(replace combined = TT4*tp) calc4(replace internalcontrol=TT1*tp) reps(`reps')
matrix eqn_p = e(REqn)
matrix joint_base[1,`outCol']= eqn_p[1,colnumb(eqn_p,"rand-t pvalue")]
matrix coeff_p= e(RCoef)
matrix coeff_base[1,`outCol']= coeff_p[rownumb(coeff_p,"structuredmeeting"),colnumb(coeff_p,"rand-t pvalue")]
matrix coeff_base[2,`outCol']= coeff_p[rownumb(coeff_p,"pairincentive"),colnumb(coeff_p,"rand-t pvalue")]
matrix coeff_base[3,`outCol']= coeff_p[rownumb(coeff_p,"combined"),colnumb(coeff_p,"rand-t pvalue")]
local outCol = `outCol'+1

		
**Balanced
randcmd ((pairincentive structuredmeeting combined) ///
	reghdfe lrpc  pairincentive structuredmeeting combined internalcontrol `persistencesample4' , absorb(manager Week) cluster(manager))	///
	, treatvars(TT2 TT3 TT4) sample ///
    groupvar(manager) calc1(replace pairincentive = TT2*tp) ///
	calc2(replace structuredmeeting = TT3*tp) ///
	calc3(replace combined = TT4*tp) calc4(replace internalcontrol= TT1*tp) reps(`reps')
matrix eqn_p = e(REqn)
matrix joint_base[1,`outCol']= eqn_p[1,colnumb(eqn_p,"rand-t pvalue")]
matrix coeff_p= e(RCoef)
matrix coeff_base[1,`outCol']= coeff_p[rownumb(coeff_p,"structuredmeeting"),colnumb(coeff_p,"rand-t pvalue")]
matrix coeff_base[2,`outCol']= coeff_p[rownumb(coeff_p,"pairincentive"),colnumb(coeff_p,"rand-t pvalue")]
matrix coeff_base[3,`outCol']= coeff_p[rownumb(coeff_p,"combined"),colnumb(coeff_p,"rand-t pvalue")]
local outCol = `outCol'+1

stop

**Both internal and external control individual fixed effects
randcmd ((pairincentive structuredmeeting combined) ///
	reghdfe lrpc     pairincentive structuredmeeting combined   internalcontrol   `persistencesample5' , absorb(ID Week) keepsingle cluster(manager))	///
	, treatvars(TT2 TT3 TT4) sample ///
    groupvar(manager) calc1(replace pairincentive = TT2*tp) ///
	calc2(replace structuredmeeting = TT3*tp) ///
	calc3(replace combined = TT4*tp) calc4(replace internalcontrol= TT1*tp) reps(`reps')	
matrix eqn_p = e(REqn)
matrix joint_base[1,`outCol']= eqn_p[1,colnumb(eqn_p,"rand-t pvalue")]
matrix coeff_p= e(RCoef)
matrix coeff_base[1,`outCol']= coeff_p[rownumb(coeff_p,"structuredmeeting"),colnumb(coeff_p,"rand-t pvalue")]
matrix coeff_base[2,`outCol']= coeff_p[rownumb(coeff_p,"pairincentive"),colnumb(coeff_p,"rand-t pvalue")]
matrix coeff_base[3,`outCol']= coeff_p[rownumb(coeff_p,"combined"),colnumb(coeff_p,"rand-t pvalue")]
local outCol = `outCol'+1

**Both internal and external control, revenue as dv 

randcmd ((pairincentive structuredmeeting combined) ///
	reghdfe revenue  pairincentive structuredmeeting combined  internalcontrol   `persistencesample3' , absorb(manager Week) cluster(manager))	///
	, treatvars(TT2 TT3 TT4) sample ///
    groupvar(manager) calc1(replace pairincentive = TT2*tp) ///
	calc2(replace structuredmeeting = TT3*tp) ///
	calc3(replace combined = TT4*tp) calc4(replace internalcontrol=TT1*tp) reps(`reps')
matrix eqn_p = e(REqn)
matrix joint_base[1,`outCol']= eqn_p[1,colnumb(eqn_p,"rand-t pvalue")]
matrix coeff_p= e(RCoef)
matrix coeff_base[1,`outCol']= coeff_p[rownumb(coeff_p,"structuredmeeting"),colnumb(coeff_p,"rand-t pvalue")]
matrix coeff_base[2,`outCol']= coeff_p[rownumb(coeff_p,"pairincentive"),colnumb(coeff_p,"rand-t pvalue")]
matrix coeff_base[3,`outCol']= coeff_p[rownumb(coeff_p,"combined"),colnumb(coeff_p,"rand-t pvalue")]
local outCol = `outCol'+1

 
mat2txt , matrix(joint_base) saving("$tablepath\jointTestPost.tex") replace
mat2txt , matrix(coeff_base) saving("$tablepath\indCoefTestPost.tex") replace
 

 
 *------------------------------------------------------------------------------*
 * Other DVs, Post Period
 *------------------------------------------------------------------------------*
randcmd ((pairincentive structuredmeeting combined) ///
	reghdfe RPH  pairincentive structuredmeeting combined  internalcontrol   `persistencesample3' , absorb(manager Week) cluster(manager))	///
	, treatvars(TT2 TT3 TT4) sample ///
    groupvar(manager) calc1(replace pairincentive = TT2*tp) ///
	calc2(replace structuredmeeting = TT3*tp) ///
	calc3(replace combined = TT4*tp) calc4(replace internalcontrol=TT1*tp) reps(`reps')
matrix eqn_p = e(REqn)
matrix joint_otherdv_base[1,2]= eqn_p[1,colnumb(eqn_p,"rand-t pvalue")] 


randcmd ((pairincentive structuredmeeting combined) ///
	reghdfe lrph  pairincentive structuredmeeting combined  internalcontrol   `persistencesample3' , absorb(manager Week) cluster(manager))	///
	, treatvars(TT2 TT3 TT4) sample ///
    groupvar(manager) calc1(replace pairincentive = TT2*tp) ///
	calc2(replace structuredmeeting = TT3*tp) ///
	calc3(replace combined = TT4*tp) calc4(replace internalcontrol=TT1*tp) reps(`reps')
matrix eqn_p = e(REqn)
matrix joint_otherdv_base[1,4]= eqn_p[1,colnumb(eqn_p,"rand-t pvalue")] 



randcmd ((pairincentive structuredmeeting combined) ///
	reghdfe RPC  pairincentive structuredmeeting combined  internalcontrol   `persistencesample3' , absorb(manager Week) cluster(manager))	///
	, treatvars(TT2 TT3 TT4) sample ///
    groupvar(manager) calc1(replace pairincentive = TT2*tp) ///
	calc2(replace structuredmeeting = TT3*tp) ///
	calc3(replace combined = TT4*tp) calc4(replace internalcontrol=TT1*tp) reps(`reps')
matrix eqn_p = e(REqn)
matrix joint_otherdv_base[1,6]= eqn_p[1,colnumb(eqn_p,"rand-t pvalue")] 
	 
 log close
